import 'package:flutter/widgets.dart';

import '../isolate/utils/ly_log.dart';

class LYSizeManager {
  // 1.基本信息
  static double physicalWidth = 0.0;
  static double physicalHeight = 0.0;
  static double screenWidth = 0.0;
  static double screenHeight = 0.0;
  static double dpr = 0.0;
  static double statusHeight = 0.0;

  static double rpx = 0.0;
  static double px = 0.0;

  static void initialize({double standardSize = 750, required BuildContext context}) {
    final view = View.of(context);
    dpr = view.devicePixelRatio;
    if (view.physicalSize.width != 0) {
      // 1.手机的物理分辨率
      physicalWidth = view.physicalSize.width;
      physicalHeight = view.physicalSize.height;
      // 3.宽度和高度
      screenWidth = physicalWidth / dpr;
      screenHeight = physicalHeight / dpr;
      // 4.状态栏高度
      statusHeight = view.padding.top / dpr;
      LYLog.d(
          '1---screenWidth-$screenWidth-screenHeight=$screenHeight-statusHeight=$statusHeight-physicalWidth=$physicalWidth-physicalHeight=$physicalHeight');
    } else {
      screenWidth = MediaQueryData.fromView(view).size.width;
      screenHeight = MediaQueryData.fromView(view).size.height;
      statusHeight = MediaQueryData.fromView(view).padding.top;
      physicalWidth = screenWidth * dpr;
      physicalHeight = screenHeight * dpr;
      LYLog.d(
          '2---screenWidth-$screenWidth-screenHeight=$screenHeight-statusHeight=$statusHeight-physicalWidth=$physicalWidth-physicalHeight=$physicalHeight');
    }

    //为横屏旋转时单独处理
    if (screenWidth > screenHeight) {
      screenWidth = screenHeight;
    }

    // 5.计算rpx的大小
    rpx = screenWidth / standardSize;
    px = screenWidth / standardSize * 2;
  }

  static double setRpx(double size) {
    return rpx * size;
  }

  static double setPx(double size) {
    return px * size;
  }
}
